package com.cgbl.service.vm

import android.app.Application
import api.WorkServiceFactory
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.model.BaseResponse
import com.robot.base.model.PageListBean
import com.robot.base.model.ProjectBean
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent

class SelectProjectViewModel(application: Application) : GLBaseViewModel(application) {

    var pageNo = 1

    private val pageSize = 10

    fun refresh() {
        pageNo = 1
        fetchProject()
    }

    fun loadMore() {
        pageNo++
        fetchProject()
    }

    private fun fetchProject() {
        //获取待启动项目
        val params = mapOf(
            "pageNo" to pageNo, "pageSize" to pageSize, "status" to 1
        )
        WorkServiceFactory.getCustomerItemInfo(params).subscribe(object :
            AbstractViewModelSubscriber<BaseResponse<PageListBean<ProjectBean>>>(this) {
            override fun onNext(t: BaseResponse<PageListBean<ProjectBean>>) {
                if (pageNo == 1) {
                    uc.refreshProjectDataEvent.postValue(t.data.list)
                } else {
                    uc.loadMoreProjectDataEvent.postValue(t.data.list)
                }
            }
        })
    }


    val uc = UIChangeEvent()

    class UIChangeEvent {

        val refreshProjectDataEvent = SingleLiveEvent<List<ProjectBean>>()

        val loadMoreProjectDataEvent = SingleLiveEvent<List<ProjectBean>>()

    }

}